package com.springboot.demo.async;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @Description：
 * @Date： Created in 2021/12/22
 * @author： jianbin.lei
 * @Auto： <a href="mailto:493168771@qq.com">jianbin.lei</a>
 */
@Slf4j
@Component
public class AsyncTaskPool {

    public static Random random =new Random();

    @Async("taskExecutor")
    public void doOne() throws Exception {
        System.out.println("任务一Start");
        long start = System.currentTimeMillis();
        //随机sleep
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("任务一：耗时：" + (end - start) + "毫秒");
    }

    @Async("taskExecutor")
    public void doTwo() throws Exception {
        System.out.println("任务二Start");
        long start = System.currentTimeMillis();
        //随机sleep
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("任务二：耗时：" + (end - start) + "毫秒");
    }

    @Async("taskExecutor")
    public void doThree() throws Exception {
        System.out.println("任务三Start");
        long start = System.currentTimeMillis();
        //随机sleep
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("任务三：耗时：" + (end - start) + "毫秒");
    }
}
